import baseUrl from "./url.js"
import store from '../store/index.js'
const request = (url, data, type, loading = true) => {
	return new Promise((resolve, reject) => {
		const token = uni.getStorageSync("easyLive-token") || ''
		const httpDefaultOpts = {
			url: baseUrl + url,
			data,
			method: type || "POST",
			dataType: "json",
			header: {
				"x-requested-with": "XMLHttpRequest",
				"content-type": "application/x-www-form-urlencoded",
				"token": token,
			},
			dataType: 'json'
		}
		if (loading) {
			uni.showLoading({
				icon: "loading",
				mask: true
			})
		}
		uni.request(httpDefaultOpts).then(res => {
			if (res.statusCode == 200) {
				if(res.data.code == 1001) {
					resolve(res.data)
				} else if (res.data.code == 301) {
					resolve(res.data)
					uni.showToast({
						title: res.data.msg,
						duration: 2000,
						icon: "none"
					})
				} else if (res.data.code == 500) {
					resolve(res.data)
					uni.removeStorageSync('easyLive-token')
					uni.navigateTo({
						url:'/pages/login/login'
					})
					uni.showToast({
						title: res.data.msg,
						duration: 2000,
						icon: "none"
					})
				} else {
					resolve(res.data)
					uni.showToast({
						title: res.data.msg,
						duration: 2000,
						icon: "none"
					})
				}
			} else {
				reject(res)
				uni.showToast({
					title: '请求异常!',
					duration: 1500,
					icon: "none"
				})
			}
			if (loading) {
				uni.hideLoading()
			}
		}).catch((response) => {
			reject(response)
			uni.hideLoading()
			uni.showToast({
				title: '请求失败!',
				duration: 1500,
				icon: "none"
			})			
		})
	})
}

export default request